package com.ysg.widget.dialog;

import android.content.Context;
import android.view.Gravity;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.widget.TextView;
import com.ysg.R;
import com.ysg.base.BaseDialog;
import com.ysg.utils.YStringUtil;

/**
 * 确认框：取消，确定
 */
public class ConfirmDialog extends BaseDialog {
    public interface OnConfirmListener{
        void onConfirmClick();
    }
    private OnConfirmListener listener;
    public void setOnConfirmListener(OnConfirmListener listener){
        this.listener = listener;
    }

    private String title, content, cancelText, confirmText;
    private TextView tvTitle, tvContent, tvCancel, tvConfirm;
    public ConfirmDialog(Context context, String content) {
        super(context);
        this.content = content;
    }

    public ConfirmDialog(Context context, String title, String content) {
        super(context);
        this.title = title;
        this.content = content;
    }

    public ConfirmDialog(Context context, String content, String cancelText, String confirmText) {
        super(context);
        this.content = content;
        this.cancelText = cancelText;
        this.confirmText = confirmText;
    }

    @Override
    public int getLayoutId() {
        return R.layout.dialog_confirm;
    }

    public void initView() {
        tvTitle = findViewById(R.id.tvTitle);
        tvContent = findViewById(R.id.tvContent);
        tvCancel = findViewById(R.id.tvCancel);
        tvConfirm = findViewById(R.id.tvConfirm);
    }

    @Override
    protected void initAnimation() {
        super.initAnimation();
        Window window = getWindow();
//        window.setWindowAnimations(R.style.dialog_animation);
    }

    @Override
    public void initData() {
        if (YStringUtil.isNotEmpty(title)){
            tvTitle.setText(title);
        }
        if (YStringUtil.isNotEmpty(content)){
            tvContent.setText(content);
        }
        if (YStringUtil.isNotEmpty(cancelText)){
            tvCancel.setText(cancelText);
        }
        if (YStringUtil.isNotEmpty(confirmText)){
            tvConfirm.setText(confirmText);
        }
    }

    @Override
    public void initListener() {
        tvCancel.setOnClickListener((v)->{
            dismiss();
        });
        tvConfirm.setOnClickListener((v)->{
            dismiss();
            if (listener != null){
                listener.onConfirmClick();
            }
        });

        //内容如果是单行，居中显示
        tvContent.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                int lineCount = tvContent.getLineCount();
                if (lineCount == 1){
                    tvContent.setGravity(Gravity.CENTER);
                }
                tvContent.getViewTreeObserver().removeOnPreDrawListener(this);
                return false;
            }
        });
    }


}
